package com.suke.czx.modules.incentive.controller;

import com.suke.czx.common.base.AbstractController;
import com.suke.czx.common.utils.ParamValidateUtil;
import com.suke.czx.common.utils.R;
import com.suke.czx.common.validate.*;
import com.suke.czx.modules.incentive.entity.IncentiveAllocation;
import com.suke.czx.modules.incentive.entity.IncentiveGenerateBo;
import com.suke.czx.modules.incentive.service.IncentiveService;
import io.swagger.annotations.Api;
import lombok.AllArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;

/**
 * 应用模块名称: 绩效
 * 代码描述: 绩效控制层
 * copyright: AUZAR ALL RIGHTS RESERVED
 * company: AUZAR
 *
 * @author: 颜米晨
 * @date: 2022/07/27 19:46
 */
@Validated
@RestController
@RequestMapping("/api/incentive")
@AllArgsConstructor
@Api(value = "IncentiveController", tags = "绩效管理")
public class IncentiveController extends AbstractController {

    private final IncentiveService incentiveService;

    /**
     * 校验绩效参数是否完整
     * @param year 年份
     * @param version 版本
     * @return VO
     */
    @RequestMapping(value = "/configVerify")
    //@PreAuthorize("hasRole('but:incentive:configVerify')")
    public R configVerify(@NotBlank String year, @NotNull Integer version) {
        return incentiveService.configVerify(year, version);
    }

    /**
     * 查询已关联绩效参数年份版本
     * @param projectId 项目id
     * @return VO
     */
    @RequestMapping(value = "/related")
    @PreAuthorize("hasRole('but:incentive:relate')")
    public R related(@NotNull Long projectId) {
        return incentiveService.related(projectId);
    }

    /**
     * 查询绩效考核参数
     * @param incentiveGenerateBo 绩效考核参数查询BO
     * @return VO
     */
    @RequestMapping(value = "/search", method = RequestMethod.POST)
    @PreAuthorize("hasRole('but:incentive:search')")
    public R search(@Validated(IncentiveEvaluationSearchGroup.class) IncentiveGenerateBo incentiveGenerateBo, BindingResult bindingResult) {
        // 入参校验结果
        if (bindingResult.hasErrors()) {
            return R.error(ParamValidateUtil.paramVilated(bindingResult));
        }
        return incentiveService.search(incentiveGenerateBo);
    }

    /**
     * 绩效考核
     * @param incentiveGenerateBo 绩效考核参数BO
     * @return VO
     */
    @RequestMapping(value = "/evaluate", method = RequestMethod.POST)
    @PreAuthorize("hasRole('but:incentive:evaluate')")
    public R evaluate(@Validated(IncentiveEvaluateGroup.class) IncentiveGenerateBo incentiveGenerateBo, BindingResult bindingResult) {
        // 入参校验结果
        if (bindingResult.hasErrors()) {
            return R.error(ParamValidateUtil.paramVilated(bindingResult));
        }
        return incentiveService.evaluate(incentiveGenerateBo, getUserId());
    }

    /**
     * 绩效分配
     * @param incentiveAllocation 绩效分配实体
     * @return VO
     */
    @RequestMapping(value = "/allocate", method = RequestMethod.POST)
    @PreAuthorize("hasRole('but:incentive:allocate')")
    public R allocate(@Validated(IncentiveAllocateGroup.class) IncentiveAllocation incentiveAllocation, BindingResult bindingResult) {
        // 入参校验结果
        if (bindingResult.hasErrors()) {
            return R.error(ParamValidateUtil.paramVilated(bindingResult));
        }
        return incentiveService.allocate(incentiveAllocation, getUserId());
    }

    /**
     * 删除绩效分配
     * @param incentiveId 绩效分配id
     * @return VO
     */
    @RequestMapping(value = "/delAllocate", method = RequestMethod.POST)
    @PreAuthorize("hasRole('but:incentive:delAllocate')")
    public R delAllocate(@NotNull Long incentiveId) {
        return incentiveService.delAllocate(incentiveId, getUserId());
    }

    /**
     * 生成绩效
     * @param incentiveGenerateBo 绩效生成入参
     * @return VO
     */
    @RequestMapping(value = "/generate", method = RequestMethod.POST)
    @PreAuthorize("hasRole('but:incentive:generate')")
    public R generator(@Validated(IncentiveGenerateGroup.class) IncentiveGenerateBo incentiveGenerateBo, BindingResult bindingResult) {
        // 入参校验结果
        if (bindingResult.hasErrors()) {
            return R.error(ParamValidateUtil.paramVilated(bindingResult));
        }
        return incentiveService.generate(incentiveGenerateBo, getUserId());
    }

    /**
     * 初始化绩效
     * @param incentiveGenerateBo 绩效生成入参
     * @return VO
     */
    @RequestMapping(value = "/initial", method = RequestMethod.POST)
    //@PreAuthorize("hasRole('but:incentive:initial')")
    public R initial(@Validated(IncentiveInitialGroup.class) IncentiveGenerateBo incentiveGenerateBo, BindingResult bindingResult) {
        // 入参校验结果
        if (bindingResult.hasErrors()) {
            return R.error(ParamValidateUtil.paramVilated(bindingResult));
        }
        return incentiveService.initial(incentiveGenerateBo);
    }

}
